#ifndef _SWD_H_
#define _SWD_H_

#include "mcuhead.h"

#define SWD_DIO_BIT BIT2
#define SWD_CLK_BIT BIT1


#ifdef SWDIO

#define SWD_DIO_SET() GPIOD->BSRR=BIT5
#define SWD_DIO_CLR() GPIOD->BSRR=(BIT5<<16)
#define SWD_DIO() (GPIOD->IDR&BIT5)

#define SWD_CLK_SET() GPIOB->BSRR=BIT5
#define SWD_CLK_CLR() GPIOB->BSRR=(BIT5<<16)

#define SWD_OUT() SETBIT(GPIOD->MODER,(1<<10))
#define SWD_IN() CLRBIT(GPIOD->MODER,(3<<10))

#else

#define SWD_DIO_SET() GPIOA->BSRR=BIT2
#define SWD_DIO_CLR() GPIOA->BSRR=(BIT2<<16)
#define SWD_DIO() (GPIOA->IDR&BIT2)

#define SWD_CLK_SET() GPIOA->BSRR=BIT1
#define SWD_CLK_CLR() GPIOA->BSRR=(BIT1<<16)

#define SWD_OUT() SETBIT(GPIOA->MODER,(1<<4))
#define SWD_IN() CLRBIT(GPIOA->MODER,(3<<4))

#endif

typedef struct {
	void(*Init)(void);
	u8(*Connect)(void);
	u8(*Setup)(void);
	u8(*Reset)(void);
	u8(*Halt)(void);
	u8(*isHalt)(void);
	u8(*Go)(void);
	u8(*Run)(void);
	u8(*SetReg)(u8 n,u32 value);
	u8(*GetReg)(u8 n,u32* value);
	u8(*WriteData)(u32 addr,u32 value);
	u8(*ReadData)(u32 addr,u32 *value);
	u8(*WriteMem)(u32 addr,s16 len,u32* data);
	u8(*ReadMem)(u32 addr,s16 len,u32* data);
	u8(*preReset)(void);
	u8(*endReset)(void);
	u8(*ReadDAP)(u8 addr,u32 *value);
	u8(*WriteDAP)(u8 addr,u32 value);
	u32(*IDCores)();
	u8(*SetDataLenght)(u8 len);
}SWDBase;

extern const SWDBase SWD;

#endif
